package com.mumu.android.tcp;

import static com.mumu.android.tcp.data.Param.*;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Date;


public class CommActivity extends BaseActivity {

    private String hostname = "192.168.1.100";
    private int port = 8000;

    private final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm:ss:SSS");

    private TextView tv_udpRemoteInfo, tv_udpLocalInfo, tv_clearLog;
    private EditText tv_sendData, tv_receiveData;
    private boolean loopReceive = false;
    private Button btn_send;
    private CheckBox checkbox_Loop;

    private MuMuTcpClient muMuTcpClient;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comm);

        hostname = REMOTE_IP;  port = REMOTE_PORT;
        String title = hostname+":"+port;
        setTitle(title);
        setToBack();

        initView();
        initObject();
        btnClick();
    }

    private void initView(){
        tv_sendData = findViewById(R.id.tv_sendData);
        tv_receiveData = findViewById(R.id.tv_receiveData);
        btn_send = findViewById(R.id.btn_send);
        tv_udpRemoteInfo = findViewById(R.id.tv_udpRemoteInfo);
        tv_udpLocalInfo = findViewById(R.id.tv_udpLocalInfo);
        tv_clearLog = findViewById(R.id.tv_clearLog);
        checkbox_Loop = findViewById(R.id.checkbox_Loop);

        tv_udpRemoteInfo.setText(hostname + ":" + port);
    }

    private void initObject(){
        muMuTcpClient = new MuMuTcpClient(hostname, port, new MuMuTcpClient.TcpClientListener() {
            @Override
            public void onMessageReceived(String message) {

                String s = "》收《 ["+simpleDateFormat.format(new Date())+ "] ："+message;
                setLog(tv_receiveData, s, LogType.WARNING, false);
            }

            @Override
            public void onMessageSend(String message) {
                String s = "《发》 ["+simpleDateFormat.format(new Date())+ "] ："+message;
                setLog(tv_receiveData, s, LogType.DEFAULT, false);
            }

            @Override
            public void onConnectionStatusChanged(boolean isConnected) {
                if (isConnected){
                    setLog(tv_receiveData, hostname+" 连接成功！", LogType.SUCCEED, false);
                    tv_udpLocalInfo.setText(muMuTcpClient.getLocalAddress() + ":" + muMuTcpClient.getLocalPort());
                } else {
                    setLog(tv_receiveData, hostname+" 连接失败", LogType.ERROR, false);
                }
            }

            @Override
            public void onConnectionError(String errorMessage) {
                setLog(tv_receiveData, hostname+" 连接意外断开", LogType.ERROR, false);
                showToast(hostname+"连接已断开！");
            }
        });
        muMuTcpClient.connect();
        setLog(tv_receiveData, "开始连接...", LogType.DEFAULT, false);
    }



    private void tcpSend(String data) {
        if (muMuTcpClient ==null) {
            showToast("发送失败，连接异常");  return;
        }
        muMuTcpClient.sendMessage(data);
    }


    private void btnClick() {
        btn_send.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String info = tv_sendData.getText().toString();
                if (info.isEmpty()) {
                    showToast("发送的数据不能为空");
                    return;
                }
                tcpSend(info);
            }
        });

        tv_clearLog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tv_receiveData.setText("");
            }
        });


        checkbox_Loop.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                loopReceive = b;
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (muMuTcpClient !=null){
            muMuTcpClient.disconnect();
            muMuTcpClient =null;
        }
    }
}


